package com.mezz.bones.im.logic.repository.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mezz.bones.im.common.message.body.in.PrivateMessageRecordReq;
import com.mezz.bones.im.logic.mapper.IChatPrivateMessageRecordMapper;
import com.mezz.bones.im.logic.domain.entity.ChatPrivateMessageRecord;
import com.mezz.bones.im.logic.repository.IChatPrivateMessageRecordRepository;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mezz
 * @since 2024-12-20
 */
@Service
public class ChatPrivateMessageRecordRepositoryImpl extends ServiceImpl<IChatPrivateMessageRecordMapper, ChatPrivateMessageRecord> implements IChatPrivateMessageRecordRepository {

    @Override
    public boolean existsMsgKey(String msgKey) {

        return lambdaQuery().eq(ChatPrivateMessageRecord::getMsgKey, msgKey).last(" limit 1 ").count() > 0;
    }

    @Override
    public List<ChatPrivateMessageRecord> getUserPrivateMessagePage(PrivateMessageRecordReq req) {

        if(req.getFetchNum() == null){
            req.setFetchNum(20L);
        }

        if(req.getFetchMode()  == null){
            req.setFetchMode(1);
        }

        LambdaQueryWrapper<ChatPrivateMessageRecord> q = new LambdaQueryWrapper<>();
        q.eq(ChatPrivateMessageRecord::getUserId,req.getUserId());
        q.eq(ChatPrivateMessageRecord::getPeerId,req.getPeerId());
        q.eq(StrUtil.isNotBlank(req.getMsgKey()),ChatPrivateMessageRecord::getMsgKey,req.getMsgKey());
        q.eq(StrUtil.isNotBlank(req.getContentType()),ChatPrivateMessageRecord::getContentType,req.getContentType());
        q.eq(ChatPrivateMessageRecord::getUserDel,0);

        if(Objects.nonNull(req.getCursorId())){

            //历史数据
            q.lt(req.getFetchMode() == 0, ChatPrivateMessageRecord::getMsgSeq,req.getCursorId());

            //最新数据(可能网络问题 重新拉取数据信息 )
            q.gt(req.getFetchMode()  == 1, ChatPrivateMessageRecord::getMsgSeq,req.getCursorId());

        }

        q.orderByDesc(ChatPrivateMessageRecord::getMsgSeq);
        q.orderByDesc(ChatPrivateMessageRecord::getCreateTime);
        q.last(" limit " + req.getFetchNum());

        return this.list(q);
    }

    @Override
    public void userMsgRemove(String msgId) {

        ChatPrivateMessageRecord update = new ChatPrivateMessageRecord();
        update.setId(msgId);
        Date now = new Date();
        update.setUpdateTime(now);
        update.setUserDel(1);
        update.setUserDelTime(now);
        this.updateById(update);
    }

    @Override
    public ChatPrivateMessageRecord getMsgByUserIdAndKey(String userId, String msgKey) {
        return lambdaQuery().eq(ChatPrivateMessageRecord::getUserId,userId).eq(ChatPrivateMessageRecord::getMsgKey,msgKey).getEntity();
    }
}
